package projeto.lp2.grupo6.view;

import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import projeto.lp2.grupo6.view.utilidades.FiltroArquivo;

/**
 * Aluno: 
 *      Guilherme Monteiro 
 * 	Italo Guedes 
 * 	Tiago Leite
 * 
 * @author guilhermemg
 * @author tiagoln
 * @author italogas
 */
public class JDialogCarregaArquivos extends javax.swing.JDialog {

    /**
     * 
     */
    private static final long serialVersionUID = -8523913912001529557L;
    private JFileChooser selecionadorDeArquivos;
    private JFrameCorpoPrincipal corpoPrincipal; // observer pattern

    /** Creates new form JDialogCarregaArquivos */
    public JDialogCarregaArquivos(JFrameCorpoPrincipal pai) {
        corpoPrincipal = pai;
        selecionadorDeArquivos = new JFileChooser();
        selecionadorDeArquivos.setFileFilter(new FiltroArquivo());
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelCamposArquivos = new javax.swing.JPanel();
        jLabelArquivoUsuarios = new javax.swing.JLabel();
        jLabelArquivoEstabelecimentos = new javax.swing.JLabel();
        jTextCaminhoUsuario = new javax.swing.JTextField();
        jButtonSelUsuario = new javax.swing.JButton();
        jButtonSelEstabelecimento = new javax.swing.JButton();
        jTextCaminhoEstabelecimento = new javax.swing.JTextField();
        jButtonOk = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Sistema de Recomendacao de Almoco");
        setModal(true);
        setResizable(false);

        jPanelCamposArquivos.setBorder(javax.swing.BorderFactory.createTitledBorder("Selecione os arquivos a serem carregados"));

        jLabelArquivoUsuarios.setText("Arquivo de Opinioes dos Usuarios:");

        jLabelArquivoEstabelecimentos.setText("Arquivo dos Estabelecimentos:");

        jButtonSelUsuario.setText("...");
        jButtonSelUsuario.setToolTipText("Clique para navegar ate o arquivo.");
        jButtonSelUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSelUsuarioActionPerformed(evt);
            }
        });

        jButtonSelEstabelecimento.setText("...");
        jButtonSelEstabelecimento.setToolTipText("Clique para navegar ate o arquivo.");
        jButtonSelEstabelecimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSelEstabelecimentoActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelCamposArquivosLayout = new org.jdesktop.layout.GroupLayout(jPanelCamposArquivos);
        jPanelCamposArquivos.setLayout(jPanelCamposArquivosLayout);
        jPanelCamposArquivosLayout.setHorizontalGroup(
            jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelCamposArquivosLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabelArquivoEstabelecimentos)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabelArquivoUsuarios))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jTextCaminhoEstabelecimento, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                    .add(jTextCaminhoUsuario, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(jButtonSelEstabelecimento, 0, 0, Short.MAX_VALUE)
                    .add(jButtonSelUsuario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 49, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelCamposArquivosLayout.setVerticalGroup(
            jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanelCamposArquivosLayout.createSequentialGroup()
                .add(jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelArquivoUsuarios)
                    .add(jTextCaminhoUsuario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButtonSelUsuario))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanelCamposArquivosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelArquivoEstabelecimentos)
                    .add(jButtonSelEstabelecimento)
                    .add(jTextCaminhoEstabelecimento, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButtonOk.setText("OK");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanelCamposArquivos, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButtonCancelar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonOk, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanelCamposArquivos, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButtonOk)
                    .add(jButtonCancelar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-569)/2, (screenSize.height-213)/2, 569, 213);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSelUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelUsuarioActionPerformed
        int returnVal = selecionadorDeArquivos.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = selecionadorDeArquivos.getSelectedFile();
            jTextCaminhoUsuario.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_jButtonSelUsuarioActionPerformed

    private void jButtonSelEstabelecimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelEstabelecimentoActionPerformed
        int returnVal = selecionadorDeArquivos.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = selecionadorDeArquivos.getSelectedFile();
            jTextCaminhoEstabelecimento.setText(file.getAbsolutePath());
        }
    }//GEN-LAST:event_jButtonSelEstabelecimentoActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        boolean carregadoComSucesso = false;
        if (jTextCaminhoEstabelecimento.getText().isEmpty() || jTextCaminhoUsuario.getText().isEmpty()
                || jTextCaminhoEstabelecimento.getText().trim().equals("")
                || jTextCaminhoUsuario.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Especifique o caminho para os dois arquivos!", "Aviso", JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                corpoPrincipal.carregaArquivos(jTextCaminhoUsuario.getText().trim(), jTextCaminhoEstabelecimento.getText().trim());
                carregadoComSucesso = true;
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Erro ao carregar o arquivo. " + ex.getMessage(), "Aviso", JOptionPane.WARNING_MESSAGE);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage(), "Aviso", JOptionPane.WARNING_MESSAGE);
            }
        }
        if (carregadoComSucesso) {
            dispose();
        }
    }//GEN-LAST:event_jButtonOkActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JButton jButtonSelEstabelecimento;
    private javax.swing.JButton jButtonSelUsuario;
    private javax.swing.JLabel jLabelArquivoEstabelecimentos;
    private javax.swing.JLabel jLabelArquivoUsuarios;
    private javax.swing.JPanel jPanelCamposArquivos;
    private javax.swing.JTextField jTextCaminhoEstabelecimento;
    private javax.swing.JTextField jTextCaminhoUsuario;
    // End of variables declaration//GEN-END:variables
}
